package com.qf.index.gcrdo;


import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.qf.index.gcrdo.httpclient.SmallTool;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;


@Slf4j
public class CheckDiseaseParse {

    public static void main(String[] args) throws Exception {

        // https://www.malacards.org/api/AutoCompleteApi/GetCardSuggestions post
        // https://diseases.jensenlab.org/Search?query=parkinsonism 使用这个地址
        // 检测疾病
        // 药物，数字代号：1; 基因，数字代号：2; 通路，数字代号：3; 疾病，数字代号：4
        Db use = Db.use(DSFactory.get("localdb"));
        List<Entity> query = use.query("select * from word_and_code_set_map_new where type = '疾病' and tongyi_back = '不是' and   check5 is not null and disease is null ");
        SmallTool.printMessError("查询到的数量："+query.size());
        for (Entity entity : query) {
            String check5 = entity.getStr("check5");
            String key = entity.getStr("word");
            int id = entity.getInt("id");
            if (id == 146) {
                System.out.println("146");
            }
            if (StringUtil.isNotNull(check5)) {
                Document parse = Jsoup.parse(check5);
                if (parse!= null) {
                    Elements table = parse.select("table");
                    if (table.size() == 0) {
                        System.out.println("没匹配到");
                        entity.set("disease", "不是");

                    }else {
                        if (table.size() != 1) {
                            System.out.println("???多个表格?");
                        }
                        if (table.size() == 1) {
                            Elements tbody = table.get(0).select("tbody");
                            Elements tr = tbody.select("tr");
                            boolean isMatch = false;
                            for (Element element : tr) {
                                Element first = element.select("td").first();
                                Element type = element.select("td").get(2);
                                if (first!= null) {
                                    String text = first.text();
                                    if (text.toLowerCase().contains(key.toLowerCase())&&type.text().equals("Disease")) {
                                        System.out.println("匹配到 可以认为是疾病");
                                        entity.set("disease", text);
                                        isMatch = true;
                                        break;
                                    }
                                }

                            }
                            if (!isMatch) {
                                entity.set("disease", "不是");
                            }
                        }
                    }
                }
            }
            use.insertOrUpdate(entity, "id");
        }
    }
}


